<?php

declare (strict_types=1);
namespace App\Model;

/**
 * @property int $id 
 * @property int $shop_id 商户id
 * @property string $product_name 商品名称
 * @property int $industry_id 分类id=行业id
 * @property \Carbon\Carbon $created_at 创建时间
 * @property \Carbon\Carbon $updated_at 修改时间
 * @property string $deleted_at 删除时间
 * @property int $create_by 创建人
 * @property int $update_by 修改人
 * @property int $delete_by 删除人
 * @property int $is_show 0隐藏  1显示
 * @property int $sales_initial 初始销量
 * @property int $sales_actual 实际销量
 * @property int $is_use_coupon 可用优惠券1可用 0不可用
 * @property int $is_index 是否首页1是0否
 * @property int $is_hot 是否推荐1是0否
 * @property int $is_sell 是否上架1是0否
 * @property int $min_quantity 起订量
 * @property int $is_origin_made 是否原产地 1是 0否
 * @property int $delivery_id 运费模板id
 * @property int $center_id 原产地id
 * @property int $check_status 1已审核  0未审核  -1驳回
 * @property int $product_id 原商品id
 */
class ProductAudit extends CloudBase
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'fly_product_audit';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'reason_json'=>'array','shop_id' => 'integer', 'industry_id' => 'integer','spec_key' => 'array', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'create_by' => 'integer', 'update_by' => 'integer', 'delete_by' => 'integer', 'is_show' => 'integer', 'sales_initial' => 'integer', 'sales_actual' => 'integer', 'is_use_coupon' => 'integer', 'is_index' => 'integer', 'is_hot' => 'integer', 'is_sell' => 'integer', 'min_quantity' => 'integer', 'is_origin_made' => 'integer', 'delivery_id' => 'integer', 'center_id' => 'integer', 'check_status' => 'integer', 'product_id' => 'integer'];

    public function link_channel_audit()
    {
        $fields = ['fly_link_channel.*','fc.channel_name'];
        return $this->hasMany(LinkChannel::class,'link_id','id')
            ->leftJoin('fly_channel as fc','fc.id','=','fly_link_channel.channel_id')
            ->where('fly_link_channel.type',2)
            ->select($fields);
    }

    public function main_pic_audit()
    {
        $fields = ['tags','distribution_center_id'];
        return $this->hasMany(LinkFile::class, 'link_id' ,'id')
            ->where('fly_link_file.type',4)
            ->with('file_info')
            ->select();
    }
    public function detail_pic_audit()
    {
        $fields = ['tags','distribution_center_id'];
        return $this->hasMany(LinkFile::class, 'link_id' ,'id')
            ->where('fly_link_file.type',5)
            ->with('file_info')
            ->select();
    }
    public function shop_info()
    {
        $fields = ['shop_name','id','business_scope','business_scope','shop_desc','link_name','phone'];
        return $this->belongsTo(FlyShop::class, 'shop_id' ,'id')
            ->select($fields);
    }

    public function reason_info()
    {
        $fields = ['fly_product_audit_refuse.audit_id','fly_product_audit_refuse.reason_json','fly_product_audit_refuse.tips'];
        return $this->belongsTo(ProductAuditRefuse::class, 'id' ,'audit_id')
            ->orderBy('fly_product_audit_refuse.id','DESC')
            ->where('fly_product_audit_refuse.type','2')
            ->limit(1)
            ->select($fields);
    }
    public function refuse_info()
    {
        $fields = ['fly_product_audit_refuse.audit_id','fly_product_audit_refuse.reason_json','fly_product_audit_refuse.tips'];
        return $this->belongsTo(ProductAuditRefuse::class, 'id' ,'audit_id')
            ->orderBy('fly_product_audit_refuse.id','DESC')
            ->where('fly_product_audit_refuse.type','1')
            ->limit(1)
            ->select($fields);
    }

    /**
     * 获取审核的通过的
     * @param int $product_id
     * @param bool $cross
     * @return object
     */
    public function old_info($product_id,$cross=1): \Hyperf\Database\Model\Builder
    {
        return $this
            ->when($cross,function ($query){
                $query->where('audit_status',1);
            })
//            ->with(['center_info','landmark_info','link_channel_audit'])
//            ->where('product_id',$product_id)
            ->orderBy('id','DESC')
            ->first();
    }

    public function center_info()
    {
        $fields = ['id','name'];
        return $this->belongsTo(DistributionCenter::class,'center_id','id')->select($fields);
    }

    public function specialty_info()
    {
        $fields = ['id','specialty_name'];
        return $this->belongsTo(Specialty::class,'specialty_id','id')->select($fields);
    }
}